package com.iot.ammeter.model.parcelable

import android.os.Parcel
import android.os.Parcelable
import com.iot.ammeter.R
import com.iot.ammeter.network.NetworkHelper
import com.iot.ammeter.network.ResponseData
import kotlinx.parcelize.Parcelize
import kotlinx.parcelize.WriteWith

/**
 * 设备信息数据类
 * @param deviceId 设备ID
 * @param electricMonth 本月用电量
 * @param electricDaily 本日用电量
 * @param status 设备状态
 * @param location 设备地址
 * @see Location
 */
@Parcelize
data class DeviceInfo(
    val deviceId: String,
    val electricMonth: Float,
    val electricDaily: Float,
    val status: DeviceStatus,
    val location: @WriteWith<Location.Parceler> Location,
) : Parcelable, ResponseData

/**
 * 设备地址
 * @param school 学校名称
 * @param campus 校区
 * @param building 宿舍楼栋
 * @param dormitory 宿舍号
 */
data class Location(
    val school: String,
    val campus: String,
    val building: String,
    val dormitory: String
) {
    override fun toString(): String {
        return """
            ${school}${campus}
            ${building}${dormitory}
        """.trimIndent()
    }

    /**
     * [Location] 类型序列化支持
     */
    object Parceler : kotlinx.parcelize.Parceler<Location> {
        override fun create(parcel: Parcel): Location {
            val params = Array(4) { it.toString() }
            parcel.readStringArray(params)
            return Location(params[0], params[1], params[2], params[3])
        }

        override fun Location.write(parcel: Parcel, flags: Int) {
            val params = arrayOf(school, campus, building, dormitory)
            parcel.writeStringArray(params)
        }
    }
}

/**
 * - [NO_DEVICE] 无绑定设备
 * - [ONLINE] 在线
 * - [OFFLINE] 离线
 * - [ELECTRIC_ERROR] 用电异常
 * - [ARREARAGE] 欠费
 */
enum class DeviceStatus(val title: Int, val color: Int, val icon: Int) {
    NO_DEVICE(
        R.string.deviceStatusNoDevice,
        R.color.Offline,
        R.drawable.baseline_battery_unknown_black_36
    ),
    ONLINE(
        R.string.deviceStatusOnline,
        R.color.Success,
        R.drawable.baseline_battery_charging_full_black_36
    ),
    OFFLINE(
        R.string.deviceStatusOffline,
        R.color.Offline,
        R.drawable.baseline_power_off_black_36
    ),
    ELECTRIC_ERROR(
        R.string.deviceStatusError,
        R.color.Warn,
        R.drawable.baseline_battery_alert_black_36
    ),
    ARREARAGE(
        R.string.deviceStatusArrearage,
        R.color.Serious,
        R.drawable.baseline_battery_saver_black_36
    );
}